package com.weixin.servlet.backstage;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.service.IBackstageService;
import com.weixin.service.impl.BackstageServiceImpl;
import com.weixin.vo.XiaoLiang;

/**
 * 主页面Servlet
 * @author 唐鑫明
 *
 */
@WebServlet("/MainServlet")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IBackstageService backstageService = new BackstageServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//设置编码
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//调用业务层的方法查询所有商品的总年销量
			int yearGoodsSales = backstageService.selectTotalGoodsSales();
			//计算平均月销量
			int monthGoodsSales = (int)Math.ceil(yearGoodsSales*1.0/12);
			//调用业务层的方法查询所有商品的总年收入
			double yearGoodsPrice = backstageService.selectTotalGoodsPrice();
			//计算平均月收入
			double monthGoodsPrice = Math.ceil(yearGoodsPrice/12);
			//调用业务层的方法查询商品销量第一的信息
			XiaoLiang maxXiaoLiang = backstageService.selectMaxGoodsSales();
			//调用业务层的方法查询商品销量第二的信息
			XiaoLiang secondGoodsSales = backstageService.selectSecondGoodsSales();
			//调用业务层的方法查询商品销量第三的信息
			XiaoLiang thirdGoodsSales = backstageService.selectThirdGoodsSales();
			//调用业务层的方法查询商品销量第四的信息
			XiaoLiang fourthGoodsSales = backstageService.selectFourthGoodsSales();
			//调用业务层的方法查询销量前四的商品信息
			List<Map<String,Object>> salesGoodsEchars = backstageService.selectGoodsSalesGoodsEchars(maxXiaoLiang.getGoods_id(), secondGoodsSales.getGoods_id(),
					thirdGoodsSales.getGoods_id(), fourthGoodsSales.getGoods_id());
			//创建四个map存放四个销量前四的商品信息
			Map<String,Object>  maxMap = new HashMap<>();
			Map<String,Object>  secondMap = new HashMap<>();
			Map<String,Object>  thirdMap = new HashMap<>();
			Map<String,Object>  fourthMap = new HashMap<>();
			//准备集合存放信息
			//存放商品名的集合
			List<Object> name =new ArrayList<>();
			//商品一月销量集合
			List<Object> one = new ArrayList<>();
			//商品二月销量集合
			List<Object> two = new ArrayList<>();
			//商品三月销量集合
			List<Object> three = new ArrayList<>();
			//商品四月销量集合
			List<Object> four = new ArrayList<>();
			//商品五月销量集合
			List<Object> five = new ArrayList<>();
			//商品六月销量集合
			List<Object> six = new ArrayList<>();
			//商品七月销量集合
			List<Object> seven = new ArrayList<>();
			//商品八月销量集合
			List<Object> eight = new ArrayList<>();
			//商品九月销量集合
			List<Object> nine = new ArrayList<>();
			//商品十月销量集合
			List<Object> ten = new ArrayList<>();
			//商品十一月销量集合
			List<Object> eleven = new ArrayList<>();
			//商品十二月销量集合
			List<Object> twelve = new ArrayList<>();
			
			Iterator<Map<String, Object>> iterator = salesGoodsEchars.iterator();
			while(iterator.hasNext()) {
				Map<String, Object> map = iterator.next();
				name.add(map.get("goods_name"));
				one.add(map.get("one"));
				two.add(map.get("two"));
				three.add(map.get("three"));
				four.add(map.get("four"));
				five.add(map.get("five"));
				six.add(map.get("six"));
				seven.add(map.get("seven"));
				eight.add(map.get("eight"));
				nine.add(map.get("nine"));
				ten.add(map.get("ten"));
				eleven.add(map.get("eleven"));
				twelve.add(map.get("twelve"));
			}
			for(int i = 0;i<name.size();i++) {
				if(i==0) {
					maxMap.put("name", name.get(0));
					maxMap.put("type", "bar");
					maxMap.put("data",new ArrayList<>());
				}else if(i==1) {
					secondMap.put("name", name.get(1));
					secondMap.put("type", "bar");
					secondMap.put("data",new ArrayList<>());
				}else if(i==2) {
					thirdMap.put("name", name.get(2));
					thirdMap.put("type", "bar");
					thirdMap.put("data",new ArrayList<>());
				}else if(i==3) {
					fourthMap.put("name", name.get(3));
					fourthMap.put("type", "bar");
					fourthMap.put("data",new ArrayList<>());
				}
			}
				
			for(int j = 0;j<one.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(one.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(one.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(one.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(one.get(3));
				}
			}	
				
			for(int j = 0;j<two.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(two.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(two.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(two.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(two.get(3));
				}
			}	
			for(int j = 0;j<three.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(three.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(three.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(three.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(three.get(3));
				}
			}	
			for(int j = 0;j<four.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(four.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(four.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(four.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(four.get(3));
				}
			}	
			for(int j = 0;j<five.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(five.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(five.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(five.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(five.get(3));
				}
			}
			for(int j = 0;j<six.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(six.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(six.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(six.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(six.get(3));
				}
			}
			for(int j = 0;j<seven.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(seven.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(seven.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(seven.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(seven.get(3));
				}
			}
			for(int j = 0;j<eight.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(eight.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(eight.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(eight.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(eight.get(3));
				}
			}
			for(int j = 0;j<nine.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(nine.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(nine.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(nine.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(nine.get(3));
				}
			}
			for(int j = 0;j<ten.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(ten.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(ten.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(ten.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(ten.get(3));
				}
			}
			for(int j = 0;j<eleven.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(eleven.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(eleven.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(eleven.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(eleven.get(3));
				}
			}	
			for(int j = 0;j<twelve.size();j++) {
				if(j==0) {
					((List)maxMap.get("data")).add(twelve.get(0));
				}else if(j==1) {
					((List)secondMap.get("data")).add(twelve.get(1));
				}else if(j==2) {
					((List)thirdMap.get("data")).add(twelve.get(2));
				}else if(j==3) {
					((List)fourthMap.get("data")).add(twelve.get(3));
				}
			}
			 List<Map<String,Object>> saList  = new ArrayList<>();
			 saList.add(maxMap);
			 saList.add(secondMap);
			 saList.add(thirdMap);
			 saList.add(fourthMap);
			
			response.getWriter().println("[[{\"yearGoodsSales\":"+yearGoodsSales+",\"yearGoodsPrice\":"+yearGoodsPrice+",\"monthGoodsSales\":"+monthGoodsSales+",\"monthGoodsPrice\":"+monthGoodsPrice+"}"+","
							+new Gson().toJson(maxXiaoLiang)+","+new Gson().toJson(secondGoodsSales)+","+new Gson().toJson(thirdGoodsSales)+
							","+new Gson().toJson(fourthGoodsSales)+"],"+new Gson().toJson(name)+","+new Gson().toJson(saList)+"]");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
